package qf.module.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import qf.common.redis.constant.RedisConstant;
import qf.common.redis.utils.RedisUtil;
import qf.module.system.dao.SysUserDao;
import qf.module.system.entitiy.SysUser;
import qf.module.system.service.SysRightsService;
import qf.module.system.service.SysRoleService;
import qf.module.system.service.SysUserService;
import qf.module.system.vo.SysUserVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao userDao;

    @Resource
    private SysRightsService rightsService;

    @Resource
    private SysRoleService roleService;

    @Resource
    private RedisUtil redisUtil;


    @Override
    public SysUserVO loginTokenByUserNameAndPassowrd(String userName, String password) {
        if (StringUtils.isEmpty(userName)) {
            throw new RuntimeException("账户信息不能为空");
        }
        // 获取账户信息是否存在
        if (!Objects.isNull(redisUtil.get(RedisConstant.USER_LOGIN_INFO_KEY + userName))) {
            return redisUtil.get(RedisConstant.USER_LOGIN_INFO_KEY + userName,SysUserVO.class);
        }
        SysUser doTemp = new SysUser();
        doTemp.setUserName(userName);
        doTemp.setPassword(password);
        doTemp =  userDao.oAuthSysUser(doTemp);
        if (ObjectUtils.isEmpty(doTemp)) {
            throw new RuntimeException("账户不存在");
        }
        // 获取账户权限信息
        List<String> rightsName = rightsService.queryRightsNameByUserId(doTemp.getId());

        SysUserVO sysUserVO = JSONObject.parseObject(JSONObject.toJSONString(doTemp),SysUserVO.class);
        sysUserVO.setUserName(userName);
        sysUserVO.setRightsName(rightsName);
        // 保存redis内容
        redisUtil.set(RedisConstant.USER_LOGIN_INFO_KEY + userName,JSONObject.toJSONString(sysUserVO),RedisConstant.USER_LOGIN_INFO_EXPIRE_TIME);
        return sysUserVO;
    }

    @Override
    public SysUserVO loginTokenByBusiNoAndSecretkey(String busiNo, String secretKey) {
        // 获取账户信息是否存在
        if (!Objects.isNull(redisUtil.get(RedisConstant.USER_LOGIN_INFO_KEY + busiNo))) {
            return redisUtil.get(RedisConstant.USER_LOGIN_INFO_KEY + busiNo,SysUserVO.class);
        }
        SysUser doTemp = new SysUser();
        doTemp.setBusiNo(busiNo);
        doTemp.setSecretKey(secretKey);
        doTemp =  userDao.oAuthSysUser(doTemp);
        // 获取账户权限信息
        List<String> rightsName = rightsService.queryRightsNameByUserId(doTemp.getId());

        SysUserVO sysUserVO = new SysUserVO();
        sysUserVO.setUserName(doTemp.getUserName());
        sysUserVO.setPassword(doTemp.getPassword());
        sysUserVO.setRightsName(rightsName);
        redisUtil.set(RedisConstant.USER_LOGIN_INFO_KEY + busiNo,JSONObject.toJSONString(sysUserVO),RedisConstant.USER_LOGIN_INFO_EXPIRE_TIME);
        return sysUserVO;
    }

    @Override
    @Transactional
    public SysUser save(SysUserVO user) {
        // 通过账户查询是否存在数据
        SysUser doTemp = new SysUser();
        doTemp.setUserName(user.getUserName());
        SysUser sysUser = userDao.oAuthSysUser(doTemp);
        if (!Objects.isNull(sysUser)) {
            throw new RuntimeException("已存在数据，请勿重复保存");
        }

        // 保存角色信息
        doTemp = saveSysUserEntity(user);

        // 判断是否当前账号是否有角色关联
        if (user.getRoleIds() != null && user.getRoleIds().size() != 0) {
            roleService.saveUserAndRole(doTemp.getId(),user.getRoleIds());
        }
        return doTemp;
    }

    private SysUser saveSysUserEntity(SysUserVO user) {
        SysUser doTemp = new SysUser();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String newPassword = bCryptPasswordEncoder.encode(user.getPassword());
        doTemp.setCreateTime(System.currentTimeMillis());
        doTemp.setUpdateTime(System.currentTimeMillis());
        doTemp.setOauthExpTime(System.currentTimeMillis());
        doTemp.setUserName(user.getUserName());
        doTemp.setPassword(newPassword);
        doTemp.setFlg(SysUser.FLG_DOWN);
        doTemp.setEnabled(SysUser.STAT_ENABLED);
        doTemp.setParentId(user.getParentId());

        userDao.save(doTemp);
        return doTemp;
    }
}
